import UseSqliteFunByMsg from '@/uni_modules/mc-ui/components/m-sqlite/js_sdk/sqlite-use.js';

// 消息状态管理模块
const MessageStatusManager = {
	// 初始化消息状态表
	async initTable() {
		try {
			await UseSqliteFunByMsg.openSQL();

			let sql = `
			"session_id" INTEGER PRIMARY KEY AUTOINCREMENT,
			"last_checked_time" text,
			"unread_msgids" text`


			await UseSqliteFunByMsg.creatSqlRow({
				dbTable: 'user_message_status',
				sql: sql
			});
		} catch (error) {
			console.error('初始化消息状态表失败:', error);
		}
	},

	// 获取用户消息状态
	async getStatus(sessionId) {
		try {
			console.log(UseSqliteFunByMsg.selectTableData);
			const res = await UseSqliteFunByMsg.selectTableData('user_message_status', 'session_id',
				sessionId);
			console.log(res);
			return res.data[0] || {
				session_id: sessionId,
				last_checked_time: '1970-01-01T00:00:00Z',
				unread_msgids: '[]'
			};
		} catch (error) {
			console.error('获取消息状态失败:', error);
			throw error;
		}
	},

	// 更新用户消息状态
	async updateStatus(sessionId, status) {
		try {
			await UseSqliteFunByMsg.insertOrReplaceData('user_message_status',
				`('${sessionId}', '${status.last_checked_time}', '${status.unread_msgids}')`,
				'(session_id, last_checked_time, unread_msgids)'
			);
		} catch (error) {
			console.error('更新消息状态失败:', error);
			throw error;
		}
	}

};

export default MessageStatusManager;